from appengine_django.models import BaseModel
from google.appengine.ext import db
from google.appengine.api import users

from helpers.misc import new_skey
from people.models import Invite

class Publication(BaseModel):
	s_key =  db.StringProperty(multiline=False, required=False, indexed=True)
	author = db.UserProperty(required=False)
	title = db.StringProperty(multiline=False, required=True, indexed=False)
	description = db.StringProperty(multiline=True, required=True, indexed=False)

	public = db.BooleanProperty(required=True, default=False)
	rating = db.RatingProperty(required=False)
	status = db.IntegerProperty(required=True, default=1)
	#updated/created time
	#TODO:	other change information will be placed into a log model
	updated =  db.DateTimeProperty(auto_now=True)

        def is_owner(self):
                if users.get_current_user() == self.author: return True
                return False

        def can_edit(self):
                if self.is_owner() or \
		Invite.all().filter('user =', users.get_current_user()).filter('entry =',
                        self).filter('permission =', 2).count(): return True
                return False

        def can_view(self):
                if self.public or self.is_owner() or  \
		Invite.all().filter('entry =', self).filter('user =', 
		users.get_current_user()).filter('permission >', 0).count(): return True
                return False

        def can_comment(self):
                if self.is_owner() or \
		Invite.all().filter('user =', users.get_current_user()).filter('entry =',
                        self).filter('permission =', 1).count(): return True
                return False

	def save(self):
		if not self.author: self.author = users.get_current_user()
		if not self.s_key: self.s_key = new_skey(self)
		return super(Publication, self).save()

        def get_absolute_url(self): return "publication/%s" % self.s_key


	def __unicode__(self):
		return self.title

class Issue(BaseModel):
	s_key =  db.StringProperty(multiline=False, required=False, indexed=True)
	author = db.UserProperty(required=False)
	publication = db.ReferenceProperty(required=True)
	title = db.StringProperty(multiline=False, required=True,indexed=False)
	number = db.IntegerProperty(required=True)
	#0 - disabled, no one can see, 
	#1 - enabled but not commited, editors and author can see, 
	#2 - commited/publicated, no one can edit it
	status = db.IntegerProperty(required=True, default=1)
	updated =  db.DateTimeProperty(auto_now=True)

        def is_owner(self):
                if users.get_current_user() == self.author: return True
                return False

        def can_edit(self):
                if (self.is_owner() or \
                Invite.all().filter('user =', users.get_current_user()).filter('entry =',
                        self).filter('permission =', 2).count()) and \
                        self.status != 2: return True
                return False

        def can_view(self):
                if self.is_owner() or  \
                ( \
			self.status == 2 and Invite.all().filter('entry =', self).filter('user =', 
                	users.get_current_user()).filter('permission >', 0).count() \
		) or \
		(
			self.status > 0 and Invite.all().filter('entry =', self).filter('user =',
                        users.get_current_user()).filter('permission =', 2).count()
		):return True
                return False

        def can_comment(self):
                if self.is_owner() or \
                Invite.all().filter('user =', users.get_current_user()).filter('entry =',
                        self).filter('permission =', 1).count(): return True
                return False

	def __unicode__(self):
		return self.title

        def save(self):
                if not self.author: self.author = users.get_current_user()
                if not self.s_key: self.s_key = new_skey(self)
                return super(Issue, self).save()

        def get_absolute_url(self): return "issue/%s" % self.s_key



class Article(BaseModel):
	s_key =  db.StringProperty(multiline=False, required=False, indexed=True)
	author = db.UserProperty(required=False)
	parents = db.ListProperty(db.Key)
	title = db.StringProperty(multiline=False, required=True,indexed=False)
	#text content
	content = db.StringProperty(multiline=True, required=False, indexed=False)
	tags = db.StringListProperty()
	public = db.BooleanProperty(required=True, default=True)
	#0 - disabled, no one can see, 
	#1 - enabled but not commited, editors and author can see, 
	#2 - commited/publicated, no one can edit it
	status = db.IntegerProperty(required=True, default=1)
	rating = db.RatingProperty(required=False)
	#TODO:	other change information will be placed into a log model
	updated =  db.DateTimeProperty(auto_now=True)

        def is_owner(self):
                if users.get_current_user() == self.author: return True
                return False

        def can_edit(self):
                if (self.is_owner() or \
                Invite.all().filter('user =', users.get_current_user()).filter('entry =',
                        self).filter('permission =', 2).count()) and \
			self.status != 2: return True
                return False

        def can_view(self):
                if self.public or self.is_owner() or  \
                Invite.all().filter('entry =', self).filter('user =', 
                users.get_current_user()).filter('permission >', 0).count(): return True
                return False

        def can_comment(self):
                if self.is_owner() or \
                Invite.all().filter('user =', users.get_current_user()).filter('entry =',
                        self).filter('permission =', 1).count(): return True
                return False

	def __unicode__(self):
		return self.title

        def save(self):
                if not self.author: self.author = users.get_current_user()
                if not self.s_key: self.s_key = new_skey(self)
                return super(Article, self).save()

        def get_absolute_url(self): return "article/%s" % self.s_key


class Comment(BaseModel):
	s_key =  db.StringProperty(multiline=False, required=False, indexed=True)
	author = db.UserProperty(required=False)
	#the comment itself
	content = db.StringProperty(multiline=True, required=True, indexed=False)
	#what was commentd (Article/Publication, or a Reply to  a comment)
	commented = db.ReferenceProperty(required=True)
	#updated/created time
	#TODO:	other change information will be placed into a log model
	updated =  db.DateTimeProperty(auto_now=True)

        def save(self):
                if not self.author: self.author = users.get_current_user()
                if not self.s_key: self.s_key = new_skey(self)
                return super(Comment, self).save()

        def get_absolute_url(self):
                return "comment/%s" % self.s_key

class Image(BaseModel):
	s_key =  db.StringProperty(multiline=False, required=False, indexed=True)
	author = db.UserProperty(required=False)
	title = db.StringProperty(multiline=False, required=True, indexed=True)
	parent_ = db.ReferenceProperty(required=True)
	image = db.BlobProperty(required=True)
	thumbnail = db.BlobProperty(required=True)
	tags = db.StringListProperty(indexed=False)
	status = db.IntegerProperty(required=True, default=1)
	public = db.BooleanProperty(required=True, default=False)
	updated =  db.DateTimeProperty(auto_now=True)

        def is_owner(self):
                if users.get_current_user() == self.author: return True
                return False
        def can_view(self):
                if self.public or self.is_owner() or  \
                self.parent_.can_view(): return True
                return False

	def save(self):
                if not self.author: self.author = users.get_current_user()
                if not self.s_key: self.s_key = new_skey(self)
		return super(Image, self).save()

        def get_absolute_url(self): return "image/%s" % self.s_key
